{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "0",
   "metadata": {},
   "source": [
    "# Create voice donation sentences\n",
    "\n",
    "When people donate their voice, we ask them to read some verification sentences so that it's not possible to just use a pre-made recording.\n",
    "\n",
    "This notebook selects sentences by filtering from CommonVoice.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Download here:\n",
    "# https://commonvoice.mozilla.org/en/datasets select \"Common Voice Delta Segment 21.0\"\n",
    "df = pd.read_csv(\n",
    "    \"./data/cv-corpus-21.0-delta-2025-03-14/en/validated_sentences.tsv\", sep=\"\\t\"\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3",
   "metadata": {},
   "outputs": [],
   "source": [
    "def count_uppercase_letters(s):\n",
    "    return sum(1 for c in s if c.isupper())\n",
    "\n",
    "\n",
    "def is_ascii(s):\n",
    "    return all(ord(c) < 128 for c in s)\n",
    "\n",
    "\n",
    "def max_word_length(s):\n",
    "    if not s.strip():\n",
    "        return 0\n",
    "\n",
    "    return max(len(word) for word in s.split())\n",
    "\n",
    "\n",
    "def is_ok_sentence(s):\n",
    "    return (\n",
    "        is_ascii(s)\n",
    "        # Exclude long complicated words\n",
    "        and max_word_length(s) <= 10\n",
    "        # Proper names might be difficult to pronounce to non-native speakers\n",
    "        # and are harder to check automatically, so we exclude them. The one capital\n",
    "        # letter allowed is for the first letter of the sentence.\n",
    "        and count_uppercase_letters(s) == 1\n",
    "        and 30 <= len(s) <= 80\n",
    "        # No questions or exclamations\n",
    "        and s[-1] == \".\"\n",
    "    )\n",
    "\n",
    "\n",
    "df[\"is_ok_sentence\"] = df[\"sentence\"].apply(is_ok_sentence)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4",
   "metadata": {},
   "outputs": [],
   "source": [
    "df.loc[df[\"is_ok_sentence\"], \"sentence\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5",
   "metadata": {},
   "outputs": [],
   "source": [
    "sum(df[\"is_ok_sentence\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6",
   "metadata": {},
   "outputs": [],
   "source": [
    "sentences = df.loc[df[\"is_ok_sentence\"], \"sentence\"].tolist()[:10000]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7",
   "metadata": {},
   "outputs": [],
   "source": [
    "with open(\"../unmute/tts/voice_donation_sentences.txt\", \"w\") as f:\n",
    "    for sentence in sentences:\n",
    "        f.write(sentence + \"\\n\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".venv",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
